package com.progroup.checks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.widget.Toast;

import com.progroup.kyrsmoney.KyrsMoneyActivity;
import com.progroup.kyrsmoney.R;
import com.progroup.kyrsmoney.database.DBAdapter;
import com.progroup.kyrsmoney.parsing.ParseXml;

public class q {
	
	public static List<Object[]> list_bank_info; 
	public static Map<String, Integer> list_name_id;
	public static Map<String, Boolean> TRUE_FALSE;
	
	public static Map<String, List<Object[]>> list_bank_kyrs;
	public static String date="";
	
	public static void getBanksInfo(Context context){
		
		DBAdapter db = new DBAdapter(context);
		try{
			db.open();
		Cursor cursor = db.DB_GET_BANKS_INFO();
		list_bank_info = new ArrayList<Object[]>();
		TRUE_FALSE = new HashMap<String, Boolean>();
		list_name_id = new HashMap<String, Integer>();
		if(cursor!=null){
			cursor.moveToFirst();
			int i = 0;
			do{
				list_bank_info.add(new Object[]{cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3)});
				list_name_id.put(cursor.getString(1), cursor.getInt(0));
				TRUE_FALSE.put(cursor.getString(1).toString(), Boolean.parseBoolean(cursor.getString(2)));
				//Log.i("GETBANKINFO", ""+i);
				i++;
			}while(cursor.moveToNext());
			
			
		}
		cursor.close();
		db.close();
		}catch (Exception e) {
			// TODO: handle exception
			//Checks.CToast(context, "getBanksInfo: "+e.toString());
			Log.i("GETBANKINFO", e.toString());
			try{
				db.close();
			}catch (Exception i) {
				// TODO: handle exception
			}
		}
	}
	
	public static void getBanksKyrs(Context context){
		DBAdapter db = new DBAdapter(context);
		try{
		db.open();
		Cursor cursor = db.DB_GET_BANK_KYRS_TODAY();
		Cursor cursor2 = db.DB_GET_BANK_KYRS_YESTERDAY();
		list_bank_kyrs = new HashMap<String, List<Object[]>>();
		Log.i("CURSOR2", cursor2.getCount()+"");
		Log.i("CURSOR", cursor.getCount()+"");
		List<Object[]> list_usd= new ArrayList<Object[]>();
		List<Object[]> list_eur= new ArrayList<Object[]>();
		List<Object[]> list_rub= new ArrayList<Object[]>();
        
		if(cursor2!=null&cursor2.getCount()!=0){
			//Log.i("CURSOR2", "TRUE");
			if (cursor != null&cursor.getCount()!=0) {
				//Log.i("CURSOR", "TRUE");
				cursor.moveToFirst();
				cursor2.moveToFirst();
				//t1.name, t2.usd_buy, t2.usd_sale, t2.eur_buy, t2.eur_sale, t2.rub_buy, t2.rub_sale, t2.date, t1.url
				//t1.name, t2.usd_buy, t2.usd_sale, t2.eur_buy, t2.eur_sale, t2.rub_buy, t2.rub_sale
				int i =0;
				do {
					Log.e("USD!!!!!", "USD_BUY="+cursor.getString(1)+"   USD_SALE="+cursor.getString(2));
					list_usd.add(new Object[]{
							cursor.getString(0), 
							cursor.getString(1), cursor2.getString(1), 
							cursor.getString(2), cursor2.getString(2), 
							cursor.getString(7), 
							cursor.getString(8)});
					
					
					list_eur.add(new Object[]{
							cursor.getString(0), 
							cursor.getString(3), cursor2.getString(3), 
							cursor.getString(4), cursor2.getString(4), 
							cursor.getString(7), 
							cursor.getString(8)});
					
					
					list_rub.add(new Object[]{
							cursor.getString(0), 
							cursor.getString(5), cursor2.getString(5), 
							cursor.getString(6), cursor2.getString(6), 
							cursor.getString(7), 
							cursor.getString(8)});
					//Log.e("CURSOR2", "TRUE"+(i++));
					
				} while (cursor.moveToNext()|cursor2.moveToNext());
				list_bank_kyrs.put("USD", list_usd);
				list_bank_kyrs.put("EUR", list_eur);
				list_bank_kyrs.put("RUB", list_rub);
			}
		}else{
			//Log.i("CURSOR2", "FALSE");
			if (cursor != null&cursor.getCount()!=0) {
			//	Log.i("CURSOR", "TRUE");
				cursor.moveToFirst();
				
			int i = 0;
				do {
					Log.e("USD!!!!!", "USD_BUY="+cursor.getString(1)+"   USD_SALE="+cursor.getString(2));
					list_usd.add(new Object[]{
							cursor.getString(0), 
							cursor.getString(1), "0", 
							cursor.getString(2), "0", 
							cursor.getString(7), 
							cursor.getString(8)});
					
					
					list_eur.add(new Object[]{
							cursor.getString(0), 
							cursor.getString(3), "0", 
							cursor.getString(4), "0", 
							cursor.getString(7), 
							cursor.getString(8)});
					
					
					list_rub.add(new Object[]{
							cursor.getString(0), 
							cursor.getString(5), "0", 
							cursor.getString(6), "0", 
							cursor.getString(7), 
							cursor.getString(8)});
					
					//Log.e("CURSOR2", "FALSE"+(i++));
					
				} while (cursor.moveToNext());
				list_bank_kyrs.put("USD", list_usd);
				list_bank_kyrs.put("EUR", list_eur);
				list_bank_kyrs.put("RUB", list_rub);
			}
		}
		
		
		
		cursor.close();
		cursor2.close();
		db.close();
		}catch (Exception e) {
			// TODO: handle exception
			//Checks.CToast(context, "getBanksKyrs: "+e.toString());

			Log.i("GETBANKKYRS", e.toString());
			try{
			db.close();
			}catch (Exception i) {
				// TODO: handle exception
			}
		}
	}
	
	
	public static int[] getIdMasive(){
		int[] s = new int[list_bank_info.size()];
		for(int i=0; i<list_bank_info.size();i++){
			s[i]=Integer.parseInt(list_bank_info.get(i)[0].toString());
		}
		return s;
	}
	
	public static void UpdateCheked(Context context){
		
		  DBAdapter db = new DBAdapter(context);
		  try{
    	  db.open();
    	  for(int i = 0; i<list_bank_info.size(); i++){


    		  if(!list_bank_info.get(i)[2].toString().equalsIgnoreCase(TRUE_FALSE.get(list_bank_info.get(i)[1]).toString())){
    			  
    			  
    			  db.DB_bank_true_false(list_bank_info.get(i)[1].toString(), TRUE_FALSE.get(list_bank_info.get(i)[1].toString()));
				 // Log.i("UPDATECHEKED", list_bank_info.get(i)[1].toString()+"= "+list_bank_info.get(i)[2].toString()+"/"+TRUE_FALSE.get(list_bank_info.get(i)[1].toString()));
    		  }
    		 // Log.i("UPDATECHEKED", i+"");
    	  }
    	  db.close();
		  }catch (Exception e) {
			// TODO: handle exception
			  //Checks.CToast(context, "UpdateCheked: "+e.toString());
			  Log.i("UPDATECHEKED", e.toString());
			  try{
				  db.close();
			  }catch (Exception i) {
				// TODO: handle exception
			}
		}
    	 
	}
	
	public static void Parsing(Context context){
		if(Checks.isOnline(context)){
			try{
				Log.i("PARSING", "!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  		  ParseXml parser = new ParseXml(context);
  		  parser.Parsing(getIdMasive());
			}catch (Exception e) {
				// TODO: handle exception
				Log.i("PARSINGQQQQQ", e.toString());
				//Checks.CToast(context, "Parsing: "+e.toString());
			}
  	  }else{
  		  Checks.CToast(context, context.getText(R.string.dialog_exception_internet).toString());
  	  }
	}
	

	public static int getBankID(String name){
		 return list_name_id.get(name);
	}
}
